package com.example.user.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.example.common.Interface.user.entity.dto.*;
import com.example.common.Interface.user.entity.vo.UserInfoVO;
import com.example.common.Interface.user.service.UserService;
import com.example.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @DubboReference(check = false)
    private UserService UserService;

    @PostMapping("/register")
    public ResponseResult<RegisterRespDTO> register(@RequestBody RegisterReqDTO request) {
        if(request == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = UserService.register(request);
        return ResponseResult.success(resp);
    }

    @PostMapping("/login")
    public ResponseResult<LoginRespDTO> login(@RequestBody LoginReqDTO request) {
        if(request == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = UserService.login(request);
        StpUtil.login(resp.getUserId());
        return ResponseResult.success(resp);
    }

    @PostMapping("/logout/{userid}")
    public ResponseResult<Boolean> logout(@PathVariable Long userId) {
        if(userId == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = UserService.logout(userId);
        if(!resp){
            return ResponseResult.fail("登出失败");
        }
        StpUtil.logout(userId);
        return ResponseResult.success(true);
    }

    @DeleteMapping("/delete/{userId}")
    public ResponseResult<Boolean> deleteUser(@PathVariable Long userId) {
        if(userId == null){
            return ResponseResult.fail("请求为空");
        }
        var resp =  UserService.deleteUser(userId);
        return ResponseResult.success(resp);
    }

    @PutMapping("/update/{userId}")
    public ResponseResult<Boolean> updateUser(@PathVariable Long userId, @RequestBody UpdateReqDTO user) {
        var resp= UserService.updateUser(userId, user);
        return ResponseResult.success(resp);
    }

    @GetMapping("/get/{userId}")
    public ResponseResult<UserInfoVO> getUserInfo(@PathVariable Long userId) {
        if(userId == null){
            return ResponseResult.fail("请求为空");
        }
        var resp = UserService.getUserInfo(userId);
        return ResponseResult.success(resp);
    }
}
